package logic

import (
	"context"
	"errors"
	"good/cache"
	"good/goodrpc/goodrpc"
	"good/goodrpc/internal/svc"
	"math/rand"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *goodrpc.SendSmsRequest) (*goodrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	i, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Account).Int()
	if i >= 1 {
		return nil, errors.New("一分钟内只允许发送一条验证码")
	}

	code := rand.Intn(1000)

	cache.Rdb.Set(l.ctx, "sendSms", code, time.Minute*5)
	cache.Rdb.Expire(l.ctx, "sendSms"+in.Account, time.Minute*3)

	return &goodrpc.SendSmsResponse{}, nil
}
